package com.mubolg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mubolg.AppLoginFeignClient;
import com.mubolg.constants.Constants;
import com.mubolg.constants.GlobalExceptionCode;
import com.mubolg.mapper.CustomerMapper;
import com.mubolg.pojo.domain.Customer;
import com.mubolg.pojo.domain.CustomerSummary;
import com.mubolg.pojo.domain.CustomerWallet;
import com.mubolg.pojo.param.RegisterSaveLoginParam;
import com.mubolg.service.ICustomerService;
import com.mubolg.service.ICustomerSummaryService;
import com.mubolg.service.ICustomerWalletService;
import com.mubolg.template.AppWeChatTemplate;
import com.mubolg.utils.AssertUtil;
import com.mubolg.utils.NameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;




/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author YuGeGe
 * @since 2024-08-01
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private AppWeChatTemplate appWeChatTemplate;
    @Autowired
    private AppLoginFeignClient appLoginFeignClient;
    @Autowired
    private ICustomerSummaryService customerSummaryService;
    @Autowired
    private ICustomerWalletService customerWalletService;

    @Override
    public void register(String openIdCode, String phoneCode) {
        //获取openid
        String openID = appWeChatTemplate.getOpenID(openIdCode);
        //判断是否为空
        AssertUtil.isNotNull(openID, GlobalExceptionCode.PARAM_CODE_ERROR);
        //如果已经存在
        Customer controllerByOpenId = getOne(new QueryWrapper<Customer>().eq("open_id", openID));
        AssertUtil.isNull(controllerByOpenId, GlobalExceptionCode.PARAM_PHONE_EXIST);
        //获取手机号
        String phone = appWeChatTemplate.getPhone(phoneCode);
        //业务实现
        Customer customer = new Customer();
        //如果为空那么就执行下方向数据库添加信息

        //生成随机名称
        String name = NameUtil.getName();
        //保存Controller信息
        saveCustomer(customer,openID,phone,name);
        //保存登录信息
        extracted(phone, name, openID);
        //初始化从表
        initCustomerAssociation(customer.getId());
    }
    private void extracted(String phone, String name, String openID) {
        //调用feign接口保存Login
        RegisterSaveLoginParam registerSaveLoginParam = new RegisterSaveLoginParam();
        registerSaveLoginParam.setUsername(phone);
        registerSaveLoginParam.setType(Constants.Login.TYPE_CUSTOMER);
        registerSaveLoginParam.setEnabled(true);
        registerSaveLoginParam.setAvatar(Constants.DEFAULT_HEAD_IMAGE);
        registerSaveLoginParam.setAdmin(false);
        registerSaveLoginParam.setNickName(name);
        registerSaveLoginParam.setOpenId(openID);
        registerSaveLoginParam.setPhone(phone);
        appLoginFeignClient.save(registerSaveLoginParam);
    }

    /**
     * 新增账号信息
     * @param customer
     * @param openId
     * @param phoneNumber
     */
    private void saveCustomer(Customer customer,String openId,String phoneNumber,String name) {
        customer.setSex(Constants.SEX_MAN);
        customer.setPhone(phoneNumber);
        customer.setCreateTime(new Date());
        customer.setVersion(0);
        customer.setDeleted(false);
        customer.setOpenId(openId);
        customer.setBitState(0L);
        customer.setName(name);
        customer.setLevel(Constants.Level.LEVEL_BRONZE);
        super.save(customer);
    }

    /**
     * 初始化从表数据
     * @param id
     */
    private void initCustomerAssociation(Long id) {
        CustomerSummary customerSummary = new CustomerSummary();
        customerSummary.setId(id);
        customerSummary.setTotalOrders(0);
        customerSummary.setTotalFine(new BigDecimal("0"));
        customerSummary.setVoucherNumbers(0);
        customerSummary.setCancelOrders(0);
        customerSummaryService.save(customerSummary);

        CustomerWallet customerWallet = new CustomerWallet();
        customerWallet.setId(id);
        customerWallet.setAmount(new BigDecimal("0"));
        customerWallet.setPassword("");
        customerWallet.setDeleted(false);
        customerWallet.setVersion(0);
        customerWallet.setCreateTime(new Date());
        customerWalletService.save(customerWallet);
    }

}
